{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "# 指定原文件路径和新文件路径\n",
    "# 一次转换一整个文件夹的文件\n",
    "#FILE_PATH = 'UnprocessedLaw/中华人民共和国反垄断法.txt'\n",
    "#FILE_PATH_NEW = 'ProcessedLaw/Chunked_' + FILE_PATH.split('/')[-1]\n",
    "FOLDER_PATH = 'UnprocessedLaw' \n",
    "FOLDER_PATH_PROCESSED = 'ProcessedLaw' \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_files(FOLDER_PATH, FOLDER_PATH_PROCESSED):\n",
    "    for file in os.listdir(FOLDER_PATH): # 遍历文件夹下的所有文件（或目录）\n",
    "        file_path = os.path.join(FOLDER_PATH, file)\n",
    "        if os.path.isfile(file_path):  # 获取每个文件的完整路径\n",
    "            new_file = 'chunked' + file \n",
    "            new_file_path = os.path.join(FOLDER_PATH_PROCESSED, new_file)\n",
    "            filename = os.path.splitext(os.path.basename(file_path))[0]  # 获取去掉后缀的文件名\n",
    "            add_special_symbols(filename, file_path, new_file_path)\n",
    "\n",
    "def add_special_symbols(filename, file_path, new_file_path):\n",
    "    with open(file_path, 'r', encoding='utf-8') as file:\n",
    "        content = file.readlines()\n",
    "\n",
    "    for i, line in enumerate(content):\n",
    "        cleaned_line = line.strip()  # 先去除行两端的空格\n",
    "        if cleaned_line.startswith('第') or cleaned_line.startswith('No'):\n",
    "            content[i] = '###' + str(filename) +' ' + line\n",
    "\n",
    "    with open(new_file_path, 'w') as new_file:\n",
    "        new_file.writelines(content)\n",
    "\n",
    "\n",
    "\n",
    "process_files(FOLDER_PATH, FOLDER_PATH_PROCESSED)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
